package com.ys.zero.web;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.google.gson.Gson;

@WebServlet("/loadImg.s")
@MultipartConfig
public class UploadImgServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Map<String, Object> map = new HashMap<String, Object>();
		/**
		 * 	"snapscreenActionName", "loadImg.s",
		"imageActionName", "loadImg.s", 
		"imageUrlPrefix", "",
		"imagePath", "/", 
		"imageFieldName", "file",
		"imageMaxSize", 1024*1024*5, 
		"imageAllowFiles", new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" }
		 */
		map.put("snapscreenActionName", "loadImg.s");
		map.put("imageActionName", "loadImg.s");
		map.put("imageUrlPrefix", "");
		map.put("imagePath", "/");
		map.put("imageFieldName", "file");
		map.put("imageMaxSize", 1024 * 1024 * 5);
		map.put("imageAllowFiles", new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" });

		

		//				"state", "SUCCESS",
		//			    "url", u.get("webPath"),
		//			    "title", u.get("newName"),
		//			    "original", u.get("oldName"));	

		//String zz = request.getParameter("config");//request.getQueryString();
		//String pattern = "^\\w*=(\\w*)&&.*$";
		//String xx = zz.replaceAll(pattern, "$1");
		//System.err.println(xx);
		//System.err.println(zz);
		Gson g = new Gson();
		//if ("config".equals(zz)) {
			String json = g.toJson(map);
//			System.out.println(json);
			response.getWriter().append(json);
		/*} else {
			String json = g.toJson(map1);
			System.out.println(json);
			response.getWriter().append(json);
		}*/

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//				"state", "SUCCESS",
		//			    "url", u.get("webPath"),
		//			    "title", u.get("newName"),
		//			    "original", u.get("oldName"));
		Map<String, Object> map1 = new HashMap<String, Object>();

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Part part = request.getPart("file");
		
		String realpath  = this.getServletContext().getRealPath("/");//获取项目名  Zero
		File file  = new File(realpath);
		realpath = file.getParentFile().getParentFile().getAbsolutePath();
		realpath  += "/webapps/ROOT/img";
		String filename = "" + new Date().getTime() + part.getSubmittedFileName();
		String imagesPath = "/img/" + filename;
		String filepath = realpath + "/" + filename;
//		System.out.println(filepath);
		ServletContext context = getServletContext();
		context.setAttribute("imagesPath", imagesPath);
		String webpath = "/img/" + filename;
//		System.out.println(filename);
		part.write(filepath);
		map1.put("state", "SUCCESS");
		map1.put("url", webpath);
		Gson g = new Gson();
		//if ("config".equals(zz)) {
			String json = g.toJson(map1);
//			System.out.println(json);
			response.getWriter().append(json);

	}

}
